//
//  BSStatusPictureView.swift
//  mySwiftWeibo
//
//  Created by apple on 16/7/31.
//  Copyright © 2016年 itcast沈庆伟. All rights reserved.
//

import UIKit

private let statusPictureViewItemWH = (UIScreen.width() - 2 * CellMargin - CGFloat(maxOfColumn - 1) * ItemMargin) / CGFloat(maxOfColumn)

///最大列数
private let maxOfColumn = 3

///图片之间的间距
private let ItemMargin: CGFloat = 4

class BSStatusPictureView: UIView {

   ///图片的url数组
    var imageURLs: [NSURL]? {
        didSet{
            //布局imageView
            let size = layoutImages(imageURLs)
            
            //自己来更新自己的宽高约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
        }
    }
    
    ///布局9个imageview的位置
    private func layoutImages(imageURLs: [NSURL]?) -> CGSize{
        
        //获取图片数量
        let count = imageURLs?.count ?? 0
        
        //测试
        debugLabel.text = "图片数量 = \(count)"
        
        //总列数,默认3列
        var numberOfColumn = maxOfColumn
        
        if count == 1 {
            //只有一个图片,显示1列
            numberOfColumn = 1
        }else if count == 2 || count == 4 {
            //2个或4个显示2列
            numberOfColumn = 2
        }
        
        //获取每个图片,设置位置
        for (index, imageView) in imageViews.enumerate(){
            
            if index >= count {
                //不存在的图片将其隐藏
                imageView.hidden = true
            }else{
                //显示
                imageView.hidden = false
                
                //哪列
                let column = index % numberOfColumn
                
                //哪行
                let row = index / numberOfColumn
                
                //x
                let x = CGFloat(column) * (statusPictureViewItemWH + ItemMargin)
                
                let y = CGFloat(row) * (statusPictureViewItemWH + ItemMargin)
                
                imageView.frame = CGRect(x: x, y: y, width: statusPictureViewItemWH, height: statusPictureViewItemWH)
                
                //设置imageview内容--来到这里,iamgeURLs肯定有值了
                let url = imageURLs![index]
                //SDWebImage显示图片
                imageView.sd_setImageWithURL(url)
            }
        }
        
        //根据 图片数量 计算配图视图的总大小
        if count == 0 {
            return CGSizeZero
        }
        
        //count不为 0 
        //宽度 = 总列数 * 图片宽度 + (总列数 - 1)* 图片间距
        let width = CGFloat(numberOfColumn) * statusPictureViewItemWH + (CGFloat(numberOfColumn) - 1) * ItemMargin
        
        // 2 = (6 + 3 - 1) / 3
        // 3 = (9 + 3 - 1) / 3
        // 1 = (3 + 3 - 1) / 3
        //总行数 = (总数 + 总列数 - 1)/总列数
        let numberOfRow = (count + numberOfColumn - 1) / numberOfColumn
        
        //高度 = 总行数 * 图片高度 + (总行数 - 1)*图片间距
        let height = CGFloat(numberOfRow) * statusPictureViewItemWH + (CGFloat(numberOfRow) - 1) * ItemMargin
        
        return CGSize(width: width, height: height)
    }
    
    /*****************************/
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        setupUI()
    }

    ///UI
    private func setupUI(){
        //创建9个imageview
        for _ in 0..<9 {
          let imageView = UIImageView()
            
            imageView.backgroundColor = UIColor.randomColor()
            
            //添加
            self.addSubview(imageView)
            
            //保存到数组
            imageViews.append(imageView)
        }
        
        //添加调试label
        self.addSubview(debugLabel)
        debugLabel.frame = CGRect(x: 0, y: 0, width: 300, height: 200)
    }
    
    //MARK: - 懒加载
    ///存放9张图片
    private lazy var imageViews: [UIImageView]=[UIImageView]()
    
    ///调试label
    private lazy var debugLabel: UILabel={
        let label = UILabel(textColor: UIColor.redColor(), fontSize: 20)
        
        label.text = "我是label"
        label.numberOfLines = 0
        label.hidden = true
        
        return label
    }()

}
